AddPurchaseOrderButton = Ext.extend(Ext.Button, {
	setMainWindow : function(main_win) {
		this.main_win = main_win;
	},
	purDeal : function() {
		this.sendDataAndGetMes({
			'ordersId' : this.main_win.purchase_id_hidden.getValue()
		});
	},
	
	
	/**
	 * 私有方法（插入数据）
	 */
	purBatchInsert : function() {
		if (!this.main_win.validatePurdata(this.main_win.purchase_item_store, this.main_win.partner_combox)) {
			return;
		}
		
		var recs = this.main_win.purchase_item_store.getRange();
		var datas = getRecordPropValue(recs);

		var formValues = this.main_win.getFormValues();
				
		this.sendDataAndGetMes({
			'purOrder' : formValues,
			'purDetails' : Ext.util.JSON.encode(datas)
		});
		
		this.main_win.removeLinkQuotaWindow();
	},
	
	
	
	
	/**
	 * 私有方法（修改数据）
	 */
	purBatchUpdate : function() {
		if (!this.main_win.validatePurdata(this.main_win.purchase_item_store, this.main_win.partner_combox)) {
			return;
		}

		var form_values = this.main_win.getFormValues();

		var obj = this.main_win.getModifyData();

		this.sendDataAndGetMes.createDelegate(this, [{
					'pur' : form_values,
					'all' : Ext.util.JSON.encode(obj.all),
					'add' : Ext.util.JSON.encode(obj.add),
					'update' : Ext.util.JSON.encode(obj.update)
				}])();

	},
	
	
	
	
	/**
	 * 关闭
	 */
	purClose : function() {
		this.main_win.close();
	},
	/**
	 * 订单恢复
	 */
	purRestore : function() {
		this.sendDataAndGetMes({
			'ordersId' : this.main_win.purchase_id_hidden.getValue()
		});
	},
	
	
	/**
	 * 订单删除
	 */
	purDelete : function() {
		this.sendDataAndGetMes({
			'ordersId' : this.main_win.purchase_id_hidden.getValue()
		});
	},
	
	
	
	
	/**
	 * 订单浏览
	 */
	purQuery : function() {
		var desktop = Uec3.App.getDesktop();   
		desktop.launchWindowByParam("showPurchaseOrderWindowCreator",{});
	},
	
	
	purAgreeRecieve : function() {
		this.sendDataAndGetMes({
			'ordersId' : this.main_win.purchase_id_hidden.getValue()
		});
	},
	
	
	/**
	 * 重置
	 */
	purReset : function() {
		this.main_win.purchase_panel.getForm().reset();
		this.main_win.purchase_item_store.removeAll();
//		this.main_win.partner_combox.clearValue();
		this.main_win.address_combox.clearValue();
		this.sendDataAndGetMes();
	},
	
	
	sendDataAndGetMes : function(params, notLoadedMask) {
		if (!notLoadedMask) {
			this.main_win.loadMask = new Ext.LoadMask(this.main_win.getId(), {
						removeMask : true,
						msg : '数据传送中......'
					});
			// this.loadMask.enable();
			this.main_win.loadMask.show();
//			alert('');
		}

		Ext.Ajax.request({
					url : this.url,
					params : params,
					success : function(data) {
						var vo = Ext.decode(data.responseText);
						this.vo = vo;
						
						this.main_win.status = this.vo.status;
						
						if (!this.vo.loadedMesesKeys)
							return;
						var load_funcs_keys = this.vo.loadedMesesKeys
								.split(':');
						this.vo.load_num = load_funcs_keys.length;
						if (this.vo.load_num == 1
								&& load_funcs_keys[0] == 'callbackAlertFunc') {
							this.vo.load_num = 0;
						}
						Ext.each(load_funcs_keys, function(func_key_str) {
							
							eval('this.' + func_key_str + '()');
						}.createDelegate(this));
					}.createDelegate(this)
				});
	},
	loadButtonsToolBar : function(params) {
//		alert(this.vo.status);
		this.main_win.removeOldPurButtons();
		Ext.Ajax.request({
			url : this.loadBtnsUrl,
			params : params ? params : {
				'status' : this.vo.status,
				'ordersId' : this.vo.ordersId
			},
			success : function(data) {
				var bo = Ext.decode(data.responseText);

				if (bo.disa == '1') {
					var items = this.main_win.purchase_panel.getForm().items;
					
					for(var i=0; i<items.getCount(); i++) {
						var item = items.get(i);
						if(item.xtype != 'grid') {
							item.disable();
						}
					}
								
					this.main_win.editor.disable();
					
					if(this.main_win.purchase_toolbar) {
						this.main_win.purchase_toolbar.removeAll();
						if(this.main_win.status == '7') {
							this.main_win.purchase_toolbar.add({text : '[生成入库单]', iconCls : 'icon-edit', handler : this.main_win.createRecieves.createDelegate(this.main_win)});
							this.main_win.purchase_toolbar.doLayout();
						}
					} 
					
				} else {
					var items = this.main_win.purchase_panel.getForm().items;
					
					for(var i=0; i<items.getCount(); i++) {
						var item = items.get(i);
						if(item.xtype != 'grid') {
							item.enable();
						}
					}
				}

				this.main_win.createNewPurButtons( bo.btns );
				this.loadBtns = true;
				this.callbackAlertFunc.createDelegate(this)();
			}.createDelegate(this)
		});
	},
	loadPurForm : function(params) {
		this.main_win.purchase_panel.getForm().load({
			url : this.loadOrdersUrl,
			params : params ? params : {
				'ordersId' : this.vo.ordersId
			},
			success : function(form, action) {	
				
				var record = action.result;
				var address = '地址:' + (record.data.province ? record.data.province : '') + (record.data.city ? record.data.city : '') + 
   	             	(record.data.district ? record.data.district : '') + (record.data.street ? record.data.street : '') + 
   	             	(record.data.receiverName ? ";联系人:" + record.data.receiverName : '') + 
   	             	(record.data.phone ? ";电话:" + record.data.phone : '') + 
   	             	(record.data.mobile ? ";手机:" + record.data.mobile : '');
				
				this.main_win.partner_combox.initializeValue({
					'name' : record.data.partnerNameByBuyer,
					'pyCode' : record.data.pyCode,
					'id' : record.data.partnerIdByBuyer
				});
					
				this.main_win.address_combox.initializeValue({
					'areaId' : record.data.areaId,
					'address' : address
				});
				
				this.loadForm = true;
				this.callbackAlertFunc.createDelegate(this)();
			}.createDelegate(this)
		});
	},
	loadDetailsGrid : function(params) {
		this.main_win.purchase_item_store.load({
			params : params ? params : {
				'ordersId' : this.vo.ordersId
			},
			callback : function() {
				this.loadGrid = true;
//				this.main_win.addRemovePurItemRecordEvent.createDelegate(this.main_win)();
				
				if(this.main_win.creator.ordersNameEn == 'online_purchase_order' && this.main_win.status != '-1' && 
					this.main_win.status != '0' && this.main_win.status != '2') {
					this.main_win.purchase_item_colName.setHidden(3, false);
				}
				
				this.callbackAlertFunc.createDelegate(this)();
			}.createDelegate(this)
		});
	},
	callbackAlertFunc : function() {
		
//		alert(this.vo.pur_alert_mes);
		
		if (this.vo.load_num == 1) {
			if( !this.loadBtns) {
				return;
			}
		}
		
		if(this.vo.load_num == 3) {
			if ( !this.loadForm || !this.loadGrid || !this.loadBtns) {
				return;
			}
		}
		
		
		if (this.main_win.loadMask) {
			this.main_win.loadMask.hide();
		}
		
		if(this.vo.pur_alert_mes) {
			var alerts = this.alertMeses[this.vo.pur_alert_mes].split(':');
			if( this.vo.remove == '1' ) {
				Ext.Msg.alert(alerts[0], alerts[1], function() {
					this.close();
				}.createDelegate(this.main_win));
			} else {
				this.main_win.setBuyerCode.createDelegate(this.main_win,
							[ this.main_win.purchase_panel.getForm().findField('codeOfBuyer').getValue() ])();
				if(this.main_win.status + '' == '7') {
					Ext.Msg.confirm("提示", alerts[1] + ',您是否现在就生成采购入库单?', function(btnId) {
						if(btnId == 'no') {
							return;
						} else {
							this.main_win.createRecieves();
						}
					}.createDelegate(this));
				} else {
					Ext.Msg.alert(alerts[0], alerts[1]);
				}
			}
		}
		
	}
	
});